#include <cstdlib>
#include <iostream>
#include <string>
//#include <clib.h>
#include <time.h>
#include "Ccharacter.h"
#include "Enemy.h"
#include "fight.h"
#include "database.h"
using namespace std;

#define MAX_MONEY (750)
#define MAX_DAYS (25)


// Timing function
// not used lawl i think

int sleep(int number) {
	time_t time1;
	time1 = time (NULL);

	time_t time2 = time1 + (time_t)number*1000;

	while (time2<time1){
		time1 = time (NULL);
	}
	return 0;
}


// initialization of enemies
Enemy goblin;
Enemy troll;
Enemy ninja;
Enemy black_knight;
Enemy boss;
int day;

// Function chooses fighter and goes to "fight.cpp" fight scene
void choose_fighter( Ccharacter &char1 ) {
	// depends on level
	int sum = char1.att + char1.def + char1.spd/3 + (char1.max_health-100)/5;

	if ( sum < 40 ) {

		fight(&char1, goblin);

	} else if ( sum < 60 ) { 					// 40-60
		int random = (sum - 35)/2 + rand() % 10;
		if ( random < 8 ) {
			fight(&char1, goblin);
		} else {
			fight(&char1, ninja);
		}

	} else if ( sum < 80 ) {					// 50-80
		int random = (sum - 50)/2 + rand() % 15;
		if ( random < 16 ) {
			fight(&char1, ninja);
		} else {
			fight(&char1, troll);
		}


	} else if ( sum < 120 ) {					// 80-120
		int random = (sum - 80)/2 + rand() % 15;
		if ( random < 25 ) {
			fight(&char1, troll);
		} else {
			fight(&char1, black_knight);
		}

	} else {									// 120+
		fight(&char1, black_knight);
	}
}



// Function parses available actions
int parse_action( string action, Ccharacter &char1 ) {
	if ( action == "walk" ) {
		char1.walk();					// method in Ccharacter.cpp
	} else if ( action == "rest" ) {
		day = char1.rest(day);
	} else if ( action == "fight" ) {
		choose_fighter( char1 );		// function up top ^^
	} else if ( action == "train" ) {
		char1.train();					// another method in Ccharacter.cpp
	} else if ( action == "run" ) {
		char1.run();
	} else if ( action == "meditate" ) {
		char1.meditate();
	} else if ( action == "quit" ) {
		cout << "Already? Party pooper pants. Are you sure? (Y/N)" << endl;
		string answer;
		getline ( cin, answer );
		if (answer == "N") {
			cout << "Awesome! Keep the funness going!" << endl;
		} else {
			cout << "Aww boo ok. Fine. Do whatever you want. I don't care." << endl;
			exit(0);
		}
	}
	else {
		cout << "Uknown option! Failtards" << endl;
	}
	return 0;
}



int main(int argc, char *argv[]) {

	// Initializations
	Ccharacter char1;
	string action;
	goblin.initialize("Goblin", 15, 20, 15, 100, 25);				// 30 + 3
	troll.initialize("Troll", 35, 40, 10, 125, 75);					// 70 + 2
	ninja.initialize("Ninja", 25, 15, 70, 75, 60);					// 50 + 20
	black_knight.initialize("Black Knight", 50, 50, 45, 250, 500);	// 120 + 11


	char1.initialize();
	day = 1;

	// Introduction
	srand ( time(NULL) );
	int a = rand()%5; int b = rand()%11; int c = rand()%12;
	cout << "Welcome to the "/* << intro1[a] <<*/ " of "/* << intro2[b] <<*/ "!" << endl;
	cout << "Pick a name:" << endl;
	getline ( cin, char1.name );

	cout << "----------------------" << endl;
	cout << "Welcome, " << char1.name << "!" << endl;
	cout << "Let us start the adventure!" << endl;
	cout << "----------------------" << endl;
	sleep(1);
	cout << "You open your eyes, in a daze. You notice you are in a little town of "/*<< intro3[c]*/ << endl;
	sleep(1);
	cout << "There are places to train, and monsters to slay. You stand up." << endl;
	cout << "----------------------" << endl;
	cout << "  Lets be honest here. You can ONLY train and fight and earn gold and stuff" << endl;
	cout << "  Yup. It's one of those games. You have " << MAX_DAYS << " days to find " << MAX_MONEY << " monies"<< endl;
	cout << "  Oh yeah. Rules. walk:random train:att/def run:spd meditate:hp fight:gold rest:rest quit:NEVER!" << endl;
	cout << "  Thanx for playin! lawls..." << endl;
	cout << "----------------------" << endl << endl;

while( 1 ) {

	cout << "You have " << (MAX_DAYS - day) << " days left to get " << (MAX_MONEY - char1.money) << " Gold pieces." << endl << endl;
	cout << "What would you like to do next?" << endl;
	cout << "Options: walk, train, run, meditate, fight, rest, quit" << endl;
	getline ( cin, action );

	cout << "====================" << endl;
	parse_action( action, char1 );

	cout << "====================" << endl;
	cout << "Name:    " << char1.name << endl;
	cout << "Energy:  " << char1.energy << endl;
	cout << "- - - - - - - - - -" << endl;
	cout << "Health:  " << char1.health << endl;
	cout << "Attack:  " << char1.att << endl;
	cout << "Defense: " << char1.def << endl;
	cout << "Speed:   " << char1.spd << endl;
	cout << "Gold:    " << char1.money << endl;
	cout << "====================" << endl;
	cout << "Day:     " << day << endl << endl << endl;

	if ( day >= MAX_DAYS ) {
		cout << "Your time's up! You failed!" << endl;
		cout << " GAAAAMMMEEE OOOOVVEERRRRR" << endl;
		exit(0);
	}
	if ( char1.money >= MAX_MONEY ) {
		cout << "You win! Congratulations!" << endl;
		cout << "WOOOOOOOOOO000OOOOOO0OTSSS " << endl;
		cout << "Continue? (Y/N)" << endl;
		string ans;
		getline( cin, ans );
		if( ans == "N" ) {
			exit(0);
		}
	}

}


	return EXIT_SUCCESS;
}
